﻿Imports System.Data
Imports System.Data.SqlClient
Public Class DataProvider
    Dim DataN As String = "QLHS"
    Dim ServerN As String = "."
    Private _connect As SqlConnection
    Public Property Connect() As SqlConnection
        Get
            Return _connect
        End Get
        Set(ByVal value As SqlConnection)
            _connect = value
        End Set
    End Property

    Private Function SQLConnect() As SqlConnection
        Dim cn As SqlConnection = New SqlConnection()
        'Dim connect As String = "Server=" & ServerN & "; Database=" & DataN & "; integrated Security=SSPI;"
        Dim cnst As String = "Data Source=.;Initial Catalog=QLHS;Integrated Security=True"
        cn.ConnectionString = cnst
        Return cn
    End Function

    Public Sub DataProvider()
        Try
            Connect = New SqlConnection()
            Connect = SQLConnect()
        Catch ex As Exception
            MessageBox.Show("Kiểm tra lại kết nối", "Opps!", MessageBoxButtons.OK)
        End Try
    End Sub
    Public Function Sql_ExecuteTable(ByVal sql As String) As DataTable
        Try
            Dim dt As DataTable = New DataTable()
            Connect = New SqlConnection()
            Connect = SQLConnect()
            Connect.Open()
            Dim da As New SqlDataAdapter(sql, Connect)
            da.Fill(dt)
            Connect.Close()
            Return dt
        Catch ex As Exception
        End Try
    End Function

    Public Function SQL_ExecuteNonQuery(ByVal cmd As SqlCommand) As Boolean
        Dim flag As Boolean
        Try
            Connect = New SqlConnection()
            Connect = SQLConnect()
            Connect.Open()
            cmd.Connection = Connect
            cmd.ExecuteNonQuery()
            flag = True
        Catch ex As Exception
            cmd.Connection.Close()
            flag = False
            Return flag
        End Try
        Return flag
    End Function

    Public Function SQL_ExcuteQueryReader(ByVal sql As String) As SqlDataReader
        Connect = New SqlConnection()
        Connect = SQLConnect()
        Connect.Open()
        Dim reader As SqlDataReader
        Dim cmd As SqlCommand = New SqlCommand(sql, Connect)
        Try
            cmd.Connection = Connect
            reader = cmd.ExecuteReader()
        Catch ex As Exception
            cmd.Connection.Close()
            reader = Nothing
            Return reader
        End Try
        Return reader
    End Function
End Class
